package com.fany.fastsearcher.activity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.View;
import android.widget.*;
import com.fany.constants.StringResources;
import com.fany.fastsearcher.R;
import com.fany.utils.CommonUtils;
import com.fany.utils.ConfigUtils;
import com.fany.views.controls.ListViewAdapter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by FANY on 2015/2/6.
 */
public class SettingsActivity extends ListViewActivity
{
    public static final int FILE_RESULT_CODE = 1;

    private ListViewAdapter _adapter;
    private ArrayList<HashMap<String, Object>> _items;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Button buttonSelectPath = (Button) findViewById(R.id.settings_button_selectpath);
        buttonSelectPath.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Intent intent = new Intent(SettingsActivity.this, FileSelectActivity.class);
                startActivityForResult(intent, FILE_RESULT_CODE);
            }
        });

        Button buttonRemovePath = (Button) findViewById(R.id.settings_button_removepath);
        buttonRemovePath.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                removeSelectPath();
            }
        });

        Button buttonResetPath = (Button) findViewById(R.id.settings_button_resetpath);
        buttonResetPath.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                resetSelectPath();
            }
        });

        EditText capacityText = (EditText) findViewById(R.id.settings_refresh_capacity);
        capacityText.setText(String.valueOf(ConfigUtils.getInstance().getCacheCapacity()));
        capacityText.setSelection(capacityText.getText().length());
        capacityText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                Integer value = Integer.parseInt(s.toString());
                if (value == null)
                {
                    return;
                }
                ConfigUtils.getInstance().setCacheCapacity(value);
            }
        });

        EditText spanText = (EditText) findViewById(R.id.setting_refresh_span);
        spanText.setText(String.valueOf(ConfigUtils.getInstance().getReloadCacheDaysSpan()));
        spanText.setSelection(spanText.getText().length());
        spanText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                Integer span = Integer.parseInt(s.toString());
                if (span == null)
                {
                    return;
                }
                ConfigUtils.getInstance().setReloadCacheDaysSpan(span);
            }
        });


        final Switch refreshSwitch = (Switch) findViewById(R.id.settings_refresh_switch);
        refreshSwitch.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                ConfigUtils.getInstance().setUseTimedRefresh(refreshSwitch.isChecked());
            }
        });
        final Switch pathSwitch = (Switch) findViewById(R.id.settings_switch_path);
        pathSwitch.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                ConfigUtils.getInstance().setUseCustomPath(pathSwitch.isChecked());
                if (!pathSwitch.isChecked())
                {
                    loadSearchPaths();
                }
            }
        });
        loadSearchPaths();
    }

    @Override
    protected int getListViewID()
    {
        return R.id.settings_list_path;
    }

    @Override
    protected int getContentViewID()
    {
        return R.layout.settings_activity;
    }

    private void loadSearchPaths()
    {
        _items = new ArrayList<HashMap<String, Object>>();
        ArrayList<String> rets = ConfigUtils.getInstance().getSearchPaths();
        for (String path : rets)
        {
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("image", R.drawable.folder);
            item.put("path", path);
            _items.add(item);
        }
        _adapter = new ListViewAdapter(this, _items, R.layout.settings_liststyle, new String[]{
                "image", "path"
        }, new int[]{
                R.id.settings_liststyle_image, R.id.settings_liststyle_text
        });
        getListView().setAdapter(_adapter);
    }

    private void removeSelectPath()
    {
        if (_adapter.getSelectedIndex() == -1)
        {
            CommonUtils.showToast(getApplicationContext(), StringResources.PleaseSelectFile);
            return;
        }
        if (_items.size() <= 1)
        {
            CommonUtils.showToast(getApplicationContext(), StringResources.RemainPath);
            return;
        }
        ConfigUtils.getInstance().removeSearchPath(_items.get(_adapter.getSelectedIndex()).get("path").toString());
        _items.remove(_adapter.getSelectedIndex());

        _adapter.notifyDataSetChanged();

        final Switch pathSwitch = (Switch) findViewById(R.id.settings_switch_path);
        pathSwitch.setChecked(true);
    }

    private void resetSelectPath()
    {
        ConfigUtils.getInstance().resetSearchPath();
        loadSearchPaths();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (FILE_RESULT_CODE == requestCode)
        {
            Bundle bundle = null;
            if (data != null && (bundle = data.getExtras()) != null)
            {
                HashMap<String, Object> item = new HashMap<String, Object>();
                item.put("image", R.drawable.folder);
                item.put("path", bundle.getString("path"));
                ConfigUtils.getInstance().addSearchPaths(bundle.getString("path"));
                _items.add(item);
                _adapter.notifyDataSetChanged();

                final Switch pathSwitch = (Switch) findViewById(R.id.settings_switch_path);
                pathSwitch.setChecked(true);
            }
        }
    }

    @Override
    protected void onItemClick(int index)
    {
        if (_adapter == null)
        {
            return;
        }
        _adapter.setSelectedIndex(index);
        _adapter.notifyDataSetChanged();
        setPosition(index);
    }
}
